﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using OneOfAll.Common.Bussiness;
using OneOfAll.Common.Models;
using OneOfAll.UserControls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace OneOfAll.ViewModel
{
    public class BattleViewModel : ViewModelBase
    {
        #region 字段

        #endregion

        #region 属性
        private double cameraX = 0.0;
        public double CameraX
        {
            get { return cameraX; }
            set { cameraX = value; RaisePropertyChanged(); }
        }

        private double cameraY = 0.0;
        public double CameraY
        {
            get { return cameraY; }
            set { cameraY = value; RaisePropertyChanged(); }
        }

        private double scalueValue = 1.0;
        public double ScalueValue
        {
            get { return scalueValue; }
            set { scalueValue = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<UserControl> objectList = new ObservableCollection<UserControl>();
        public ObservableCollection<UserControl> ObjectList
        {
            get { return objectList; }
            set { objectList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 命令

        #endregion

        #region 方法
        private void Test()
        {
            MonsterControl obj = new MonsterControl()
            {
                Info = new MonsterModel() { Type = ObjectTypeEnum.怪物 },
            };
            ScreenHelper.Instance.AddObject("Test", obj.Info);
            obj.Info.PropertyChanged += MonsterInfo_PropertyChanged;
            ObjectList.Add(obj);
            Canvas.SetLeft(obj, obj.Info.X);
            Canvas.SetTop(obj, obj.Info.Y);
            obj.Start();
        }


        private void MonsterInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            BaseObjectModel data = sender as BaseObjectModel;
            MonsterControl control = ObjectList.Where(t => t is MonsterControl).FirstOrDefault(t => (t as MonsterControl).Info.Key == data.Key) as MonsterControl;
            if (control == null) return;
            switch (e.PropertyName)
            {
                case "X":
                    Canvas.SetLeft(control, data.X);
                    break;
                case "Y":
                    Canvas.SetTop(control, data.Y);
                    break;
                case "IsDispose":
                    if (control.Info.IsDispose)
                        ControlDispose(control);
                    break;
            }
        }

        private void PeopleInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            BaseObjectModel data = sender as BaseObjectModel;
            PeopleControl control = ObjectList.Where(t => t is PeopleControl).FirstOrDefault(t => (t as PeopleControl).Info.Key == data.Key) as PeopleControl;
            if (control == null) return;
            switch (e.PropertyName)
            {
                case "X":
                    Canvas.SetLeft(control, data.X);
                    break;
                case "Y":
                    Canvas.SetTop(control, data.Y);
                    break;
                case "IsDispose":
                    if (control.Info.IsDispose)
                        ControlDispose(control);
                    break;
            }
        }

        private void ControlDispose(MonsterControl control)
        {
            control.Info.PropertyChanged -= MonsterInfo_PropertyChanged;
            ObjectList.Remove(control);
        }

        private void ControlDispose(PeopleControl control)
        {
            control.Info.PropertyChanged -= MonsterInfo_PropertyChanged;
            ObjectList.Remove(control);
        }

        private void RegisterMessages()
        {
            Messenger.Default.Register<Key>(this, "CameraMoveMessage", new Action<Key>(key =>
            {
                CameraMove(key);
            }));
        }

        private void CameraMove(Key key)
        {
            switch (key)
            {
                case Key.Up:
                case Key.W:
                    CameraY += 10;
                    break;
                case Key.Down:
                case Key.S:
                    CameraY -= 10;
                    break;
                case Key.Left:
                case Key.A:
                    CameraX += 10;
                    break;
                case Key.Right:
                case Key.D:
                    CameraX -= 10;
                    break;
            }
        }
        #endregion

        public BattleViewModel()
        {
            RegisterMessages();

            PeopleControl obj = new PeopleControl()
            {
                Info = new PeopleModel(),
            };
            ScreenHelper.Instance.AddObject("Test", obj.Info);
            obj.Info.PropertyChanged += MonsterInfo_PropertyChanged;
            ObjectList.Add(obj);
            Canvas.SetLeft(obj, obj.Info.X);
            Canvas.SetTop(obj, obj.Info.Y);


            Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    App.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        Test();
                    }));
                    Thread.Sleep(50);
                }
            });
    

        }


    }
}
